package cn.wildfire.chat.kit.conversation;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;
import com.sourcemuch.im.caiba.R2;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import cn.wildfire.chat.app.extendsion.classes.GroupMembersByApiRequest;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfire.chat.kit.WfcUIKit;
import cn.wildfire.chat.kit.group.AddGroupMemberActivity;
import cn.wildfire.chat.kit.group.GroupViewModel;
import cn.wildfire.chat.kit.group.RemoveGroupMemberActivity;
import cn.wildfire.chat.kit.user.UserViewModel;
import cn.wildfirechat.model.GroupInfo;
import cn.wildfirechat.model.GroupMember;
import cn.wildfirechat.remote.ChatManager;

/**
 * @Author LiZhong
 * @Date 2019/9/9
 */
public class ViewGroupMemberActivity extends WfcBaseActivity {

    @BindView(R2.id.icon_back)
    ImageView iconBack;
    @BindView(R2.id.tv_group_count)
    TextView tvGroupCount;
    @BindView(R2.id.rv_group)
    RecyclerView rvGroup;

    GroupMembersByApiRequest getGroupMembersByApi;
    String groupTarget;
    List<GroupMember> groupList;
    GroupMember ownerMember;
    String userId;
    UserViewModel userViewModel;
    private GroupViewModel groupViewModel;
    private GroupInfo groupInfo;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        groupList = new ArrayList<>();
        userId = ChatManager.Instance().getUserId();

        userViewModel = WfcUIKit.getAppScopeViewModel(UserViewModel.class);
        Intent intent = getIntent();
        groupTarget = intent.getStringExtra("GroupID");

        groupViewModel = ViewModelProviders.of(this).get(GroupViewModel.class);
        groupInfo = groupViewModel.getGroupInfo(groupTarget, true);
        tvGroupCount.setText(getString(R.string.group_member) + "(" + groupInfo.memberCount + ")");
        getGroupMembersByApi = new GroupMembersByApiRequest(this, groupTarget, new GroupMembersByApiRequest.OnGetGroupMembersListener() {
            @Override
            public void result(List<GroupMember> groupMemberList) {
                groupList.clear();
                groupList.addAll(groupMemberList);

                for (GroupMember groupMember : groupMemberList) {
                    if (userId.equals(groupMember.memberId)) {
                        ownerMember = groupMember;
                    }
                }

                //添加两个数据,以用来改变列表最后的图标
                for (int i = 0; i < 2; i++) {
//                    GroupMember groupMember = groupList.get(groupMemberList.size() - 1);
                    GroupMember groupMember = new GroupMember();
                    groupMember.lastPosition = 0;
                    groupMember.memberId = "";
                    groupMember.groupId = groupTarget;
                    groupMember.alias = "";
                    groupMember.updateDt = 0;
                    groupList.add(groupMember);
                }

                for (int i = groupList.size() - 1; i <= groupList.size(); i++) {
                    if (i == groupList.size() - 1) {
                        groupList.get(i - 1).lastPosition = 2;
                    } else {
                        groupList.get(i - 1).lastPosition = 1;
                    }

                }

                rvGroup.setLayoutManager(new GridLayoutManager(ViewGroupMemberActivity.this, 5));
                GroupMember owner = null;
                for (int i = 0; i < groupMemberList.size(); i++) {
                    if (groupMemberList.get(i).memberId.equals(userId) && groupMemberList.get(i).type == GroupMember.GroupMemberType.Normal) {
                        groupList.remove(groupList.size() - 1);
                    }
                    if (groupMemberList.get(i).memberId.equals(userId)) {
                        owner = groupMemberList.get(i);
                    }
                }

                ViewGroupMemberAdapter adapter = new ViewGroupMemberAdapter(ViewGroupMemberActivity.this, R.layout.item_group_member, groupList, userId, owner);
                rvGroup.setAdapter(adapter);
//                adapter.checkPermissionSetIcons();


            }
        });


    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_view_group_member;
    }

    @Override
    protected void onResume() {
        super.onResume();

        getGroupMembersByApi = new GroupMembersByApiRequest(this, groupTarget, new GroupMembersByApiRequest.OnGetGroupMembersListener() {
            @Override
            public void result(List<GroupMember> groupMemberList) {
                groupList.clear();
                groupList.addAll(groupMemberList);

                for (GroupMember groupMember : groupMemberList) {
                    if (userId.equals(groupMember.memberId)) {
                        ownerMember = groupMember;
                    }
                }

                //添加两个数据,以用来改变列表最后的图标
                for (int i = 0; i < 2; i++) {
//                    GroupMember groupMember = groupList.get(groupMemberList.size() - 1);
                    GroupMember groupMember = new GroupMember();
                    groupMember.lastPosition = 0;
                    groupMember.memberId = "";
                    groupMember.groupId = groupTarget;
                    groupMember.alias = "";
                    groupMember.updateDt = 0;
                    groupList.add(groupMember);
                }

                for (int i = groupList.size() - 1; i <= groupList.size(); i++) {
                    if (i == groupList.size() - 1) {
                        groupList.get(i - 1).lastPosition = 2;
                    } else {
                        groupList.get(i - 1).lastPosition = 1;
                    }

                }

                rvGroup.setLayoutManager(new GridLayoutManager(ViewGroupMemberActivity.this, 5));
                GroupMember owner = null;
                for (int i = 0; i < groupMemberList.size(); i++) {
                    if (groupMemberList.get(i).memberId.equals(userId) && groupMemberList.get(i).type == GroupMember.GroupMemberType.Normal) {
                        groupList.remove(groupList.size() - 1);
                    }
                    if (groupMemberList.get(i).memberId.equals(userId)) {
                        owner = groupMemberList.get(i);
                    }
                }

                ViewGroupMemberAdapter adapter = new ViewGroupMemberAdapter(ViewGroupMemberActivity.this, R.layout.item_group_member, groupList, userId, owner);
                rvGroup.setAdapter(adapter);

//                adapter.checkPermissionSetIcons();

            }
        });
    }

    public void onAddMemberClick() {
        Intent intent = new Intent(this, AddGroupMemberActivity.class);
        intent.putExtra("groupInfo", groupInfo);
        startActivity(intent);
    }

    public void onRemoveMemberClick() {
        if (groupInfo != null) {
            Intent intent = new Intent(this, RemoveGroupMemberActivity.class);
            intent.putExtra("groupInfo", groupInfo);
            startActivity(intent);
        }
    }

    @OnClick(R2.id.icon_back)
    public void onClick(View view) {
        if (view.getId() == R.id.icon_back) {
            finish();
        }
    }
}
